import os
import pandas as pd
from utils import df_into_db, read_sql


def get_exist_symbols():
    df = read_sql("select distinct symbol from ohlcvm_data_daily", db_name="dataloader")
    return set(df.symbol)


path = "E:\data\ohlcvm_data\daily"
files = os.listdir(path)
print(files)
count = 0
exist_symbols = get_exist_symbols()
for file in files:
    # if file != "arbzilla.csv":
    #     continue
    # print(file)
    count += 1
    print(f"{count}/{len(files)}")
    symbol = file[:-4]
    if symbol in exist_symbols:
        continue
    try:
        df = pd.read_csv(os.path.join(path, file))
        df["symbol"] = symbol
        df_into_db(df, db_name="dataloader", table_name="ohlcvm_data_daily")
    except Exception as e:
        print(f"{file}存入数据库异常:{e}")
